import moment from 'moment'

export default {
  // 文件图标
  imgSrc: (value) => {
    const index = value.lastIndexOf('.')
    const type = value.substr(index + 1)
    switch (type) {
      case 'doc': {
        return require('@/assets/fileType/doc.png')
      }
      case 'docx': {
        return require('@/assets/fileType/doc.png')
      }
      case 'xls': {
        return require('@/assets/fileType/xlsx.png')
      }
      case 'xlsx': {
        return require('@/assets/fileType/xlsx.png')
      }
      case 'pdf': {
        return require('@/assets/fileType/pdf.png')
      }
      default: {
        return require('@/assets/fileType/default.png')
      }
    }
  },
  // 文件大小
  fileSize: (limit) => {
    let size = ''
    if (limit < 1024) { // 如果小于0.1KB转化成B
      size = limit.toFixed(2) + 'B'
    } else if (limit < 1024 * 1024) { // 如果小于0.1MB转化成KB
      size = (limit / 1024).toFixed(2) + 'KB'
    } else if (limit < 1024 * 1024 * 1024) { // 如果小于0.1GB转化成MB
      size = (limit / (1024 * 1024)).toFixed(2) + 'MB'
    } else { // 其他转化成GB
      size = (limit / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
    }

    const sizestr = size + ''
    const len = sizestr.indexOf('.')
    const dec = sizestr.substr(len + 1, 2)
    if (dec === '00') { // 当小数点后为00时 去掉小数部分
      return sizestr.substring(0, len) + sizestr.substr(len + 3, 2)
    }
    return sizestr
  },
  // 最后一条聊天内容
  lastMsg: (msgs) => {
    if (msgs.length) {
      switch (msgs[msgs.length - 1].type) {
        case 'words': {
          return msgs[msgs.length - 1].msg
        }
        case 'pic': {
          return '[图片]'
        }
        case 'file': {
          return '[文件]' + msgs[msgs.length - 1].file_name
        }
        case 'voice': {
          return '[语音]'
        }
        case 'videoCall': {
          return '[视频通话]'
        }
        case 'notice': {
          return msgs[msgs.length - 1].cf_sponsor_name + '邀请您加入视频会议'
        }
      }
    } else {
      return ''
    }
  },
  // 最后一条消息联系人ID
  lastMsgUserID: (msgs) => {
    if (msgs.length) {
      return msgs[msgs.length - 1].id
    } else {
      return NaN
    }
  },
  // 处理消息时间
  msgTime: (msgs) => {
    if (msgs.length) {
      // 一天的毫秒数
      const temp = 86400000
      // 获取世界时间并转化为当前时间
      const utcTime = moment.utc(msgs[msgs.length - 1].timestamp)
      const localTime = moment(utcTime).local()
      const localYear = localTime.format('YYYY')
      const localMonth = localTime.format('MM')
      const localDate = localTime.format('DD')
      const localHours = localTime.format('HH')
      const localMinites = localTime.format('mm')

      // 获取当前时间
      const nowTime = moment(new Date())
      const nowYear = nowTime.format('YYYY')
      const nowMonth = nowTime.format('MM')
      const nowDate = nowTime.format('DD')

      // 判断是否是今天的消息
      if (localYear === nowYear && localMonth === nowMonth && localDate === nowDate) {
        // 判断是否是刚刚的消息
        if (Math.floor(((new Date(nowTime) - new Date(localTime)) / 1000)) < 60) {
          return '刚刚'
        } else if (Math.floor(((new Date(nowTime) - new Date(localTime)) / 1000)) < 600) { // 判断是否为1-10分钟内的消息
          const mins = Math.floor(((new Date(nowTime) - new Date(localTime)) / 1000) / 60)
          return mins + '分钟前'
        } else {
          return localHours + ':' + localMinites
        }
      } else if (Math.floor(new Date(localTime) / temp) === (Math.floor(new Date(nowTime) / temp) - 1)) { // 判断是否是昨天的消息
        return '昨天' + localHours + ':' + localMinites
      } else if (localYear === nowYear) { // 判断是否为今年的消息
        return localMonth + '月' + localDate + '日' + ' ' + localHours + ':' + localMinites
      } else {
        return localYear + '年' + localMonth + '月' + localDate + '日'
      }
    } else {
      return ''
    }
  },
  // 未读消息数量
  unreadCount: (msgs) => {
    if (msgs.length) {
      return msgs.filter(item => item.unread).length
    } else {
      return 0
    }
  },
  // 文件名长度裁剪
  fileNameCut: (fileName) => {
    const index = fileName.lastIndexOf('.')
    const newName = fileName.substr(0, index)
    const type = fileName.substr(index + 1)
    if (newName.length > 30) {
      const firstWords = newName.substr(0, 14)
      const lastWords = newName.substr(newName.length - 14)
      return firstWords + '...' + lastWords + '.' + type
    } else {
      return fileName
    }
  }
}
